package com.daiTest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daiTest.mapper.UserMapper;
import com.daiTest.pojo.Headline;
import com.daiTest.pojo.User;
import com.daiTest.pojo.vo.PortalVo;
import com.daiTest.service.HeadlineService;
import com.daiTest.mapper.HeadlineMapper;
import com.daiTest.untils.JwtHelper;
import com.daiTest.untils.Result;
import com.daiTest.untils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【news_headline】的数据库操作Service实现
 * @createDate 2025-03-01 12:00:07
 */
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
        implements HeadlineService {
    @Autowired
    private HeadlineMapper headlineMapper;
    @Autowired
    private JwtHelper jwtHelper;

    /*首页数据查询
     *
     * 进行分页数据查询
     * 分页数据拼接到result中
     *
     * 注意1
     * 查询需要自定义语句（自定义mapper语句）还要携带分页
     * 注意2
     * 返回的解雇List<Map>*/
    @Override
    public Result findNewPage(PortalVo portalVo) {
        //这里需要设置参数
        IPage<Map> page = new Page<>(portalVo.getPageNum(), portalVo.getPageSize());
        headlineMapper.selectMyPage(page, portalVo);

        List<Map> records = page.getRecords();//我们对应的数据集合
        Map pageData = new HashMap();
        pageData.put("pageData", records);
        pageData.put("pageNum", page.getCurrent());
        pageData.put("pageSize", page.getSize());
        pageData.put("totalPage", page.getTotal());
        pageData.put("totalPage", page.getPages());
        pageData.put("totalSize", page.getTotal());

        Map pageInfo = new HashMap();
        pageInfo.put("pageInfo", pageData);
        return Result.ok(pageInfo);
    }


    /*根据新闻id 查看全文
     *
     * 查询对应的数据即可【多表 头条和用户表和类别表 返回map即可 】
     * 浏览量+1*/
    @Override
    public Result showHeadLineDetail(Integer hid) {
        Map map = headlineMapper.queryDetailMap(hid);
        Map headLine = new HashMap();
        headLine.put("headline", map);
        //浏览量+1
        Headline headline = new Headline();
        headline.setHid(hid);
        headline.setVersion((Integer) map.get("h.version"));
        //阅读量+1
        headline.setPageViews((Integer) map.get("pageViews") + 1);
        headlineMapper.updateById(headline);

        return Result.ok(headLine);
    }

    /*发布新闻
    根据tooken查询uid
    补全数据
    把内容添加到
     则将headline对象存入数据库*/
    @Override
    public Result publish(Headline headline, String token) {
        //根据token拿到用户id
        Integer userUid = jwtHelper.getUserId(token).intValue();
        //接着将这些属性赋值给headline
        headline.setPublisher(userUid);
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());
        //把headline放入数据库
        headlineMapper.insert(headline);
        return Result.ok(null);
    }
    /*头条回显
    *根据id获取对象
    * 再把对象内容赋值给集合
    * 再用第二个集合返回给result */
    @Override
    public Result findHeadlineByHid(Integer hid) {
        Headline headline = headlineMapper.selectById(hid);
        Map map=new HashMap();
        map.put("hid",headline.getHid());
        map.put("title",headline.getTitle());
        map.put("article",headline.getArticle());
        map.put("type",headline.getType());
        Map headline1 =new HashMap<>();
        headline1.put("headline",map);
        return Result.ok(headline1);
    }

    /*修改新闻*/
    @Override
    public Result Myupdate(Headline headline) {
        //拿到乐观锁版本号
        Integer version = headlineMapper.selectById(headline.getHid()).getVersion();
        headline.setVersion(version);
        headline.setUpdateTime(new Date());
        headlineMapper.updateById(headline);

        return Result.ok(null);
    }

    /*删除新闻*//*
    @Override
    public Result removeByHid(Integer hid) {
        int rows = headlineMapper.delete;

    }*/

}




